/**
 * IK 中文分词  版本 5.0
 * IK Analyzer release 5.0
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 源代码由林良益(linliangyi2005@gmail.com)提供
 * 版权声明 2012，乌龙茶工作室
 * provided by Linliangyi and copyright 2012 by Oolong studio
 * 
 * 
 */
package org.wltea.analyzer.sample;

import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.dic.DictSegment;
import org.wltea.analyzer.dic.Dictionary;
import org.wltea.analyzer.dic.DictionaryLoader;
import org.wltea.analyzer.dic.DictionaryLoaderFactory;
import org.wltea.analyzer.lucene.IKAnalyzer;


/**
 * IKAnalyzer 示例
 * 2012-3-2
 * 
 * 以下是结合Lucene3.4 API的写法
 *
 */
public class IKAnalyzerDemo {
	public static void main(String[] args){
		//构建IK分词器，使用smart分词模式
		Analyzer analyzer = new IKAnalyzer(true,true, true);
//		DictionaryLoaderFactory.addDictionaryLoader("demo", new DictionaryLoader() {
//			public void loadMainDict(DictSegment _MainDict) {
//				_MainDict.fillSegment("while now".toCharArray());
//				_MainDict.fillSegment("送料".toCharArray());
//				_MainDict.fillSegment("great".toCharArray());
//				_MainDict.fillSegment("器".toCharArray());
//			}
//
//			public void loadStopWordDict(DictSegment _StopWordDict) {
//
//			}
//
//			public void loadQuantifierDict(DictSegment _QuantifierDict) {
//
//			}
//		});
//		DictionaryLoaderFactory.setEnableLoaderName(Arrays.asList("demo"));
		//获取Lucene的TokenStream对象
	    TokenStream ts = null;
		try {
			String text = null;
			text = "ik分词器 支持英文吗?送料!";
			//text = "这是诛仙梦幻诛仙2一个中文分词的例子，你可以直a接运行它！IKAnalyer can analysis english text too";
			ts = get(analyzer,text);
//			Dictionary.clean();
//			get(new IKAnalyzer(true,true),"送料了:Had it for a while now. It works great! Only problem is that the phone was (supposedly) a reset phone from Asia, where photos can’t be taken without the shutter sound. My shutter sound is always on and can’t be turned off (even with the volume switch off) it runs great and works great!");
			//关闭TokenStream（关闭StringReader）
			ts.end();   // Perform end-of-stream operations, e.g. set the final offset.
			//ts.close();
			//System.out.println("--------------------------");
			//text = "这是诛仙梦幻诛仙2一个中文分词的例子，你可以直a接运行它！IKAnalyer can analysis english text too";

			//ts = get(analyzer,text);
			//关闭TokenStream（关闭StringReader）
			//ts.end();
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//释放TokenStream的所有资源
			if(ts != null){
		      try {
				ts.close();
		      } catch (IOException e) {
				e.printStackTrace();
		      }
			}
	    }
		
	}

	public static TokenStream get(Analyzer analyzer,String text) throws IOException{
		TokenStream ts = analyzer.tokenStream("myfield", new StringReader(text));
		//获取词元位置属性
	    OffsetAttribute  offset = ts.addAttribute(OffsetAttribute.class); 
	    //获取词元文本属性
	    CharTermAttribute term = ts.addAttribute(CharTermAttribute.class);
	    //获取词元文本属性
	    TypeAttribute type = ts.addAttribute(TypeAttribute.class);
	    
	    //重置TokenStream（重置StringReader）
		ts.reset(); 
		//迭代获取分词结果
		while (ts.incrementToken()) {
		  System.out.println(offset.startOffset() + " - " + offset.endOffset() + " : " + term.toString() + " | " + type.type());
		}
		return ts;
	}
}
